<?php

namespace app\admin\model;

use think\Collection;
use think\facade\Cache;

class Tagsmodel extends Commonmodel {

    protected $handler;
    protected $croomTable;

    public function __construct(array $data = [])
    {
        $this->table = 'zhjs_content_tags';
        $this->pk = 'id';
        parent::__construct($data);
        $this->croomTable = 'zhjs_classroom';
        $this->handler = Cache::store('redis')->handler();
    }

    public function findAll($where = null, $pageParam = null, $num = 20){
        $whereData = [['t.is_delete' , '=', 1]];
        $paginate = ['list_rows'=>$num];

        if($where)  $whereData = array_merge($where, $whereData);
        if($pageParam = array_splice($pageParam, 1))  $paginate = array_merge($paginate, ['query'=>$pageParam]);

        $data = self::db()
            ->alias('t')
            ->leftJoin([$this->croomTable=>'cr'],'cr.id=t.cid and cr.is_delete and cr.status = 1')
            ->field('t.id, t.title, t.status, cr.name as cname, t.create_date, t.scope, t.examine_status')
            ->where($whereData)
            ->order(['t.id'=>'desc'])
            ->paginate($paginate);

        if(!$data)  return false;

        $list['page'] = $data->render();
        $dataArr = $data->toArray();
        $list['data'] = $dataArr['data'];

        return $list;
    }

    public function saveDataInfo($data, $id = null)
    {
        if(!$id = parent::saveDataInfo($data, $id))  return false;

        $this->handler->hset('tags', $id, $data['title']);
        return true;
    }

    public function del($id)
    {
        if(!parent::del($id))  return false;

        $this->handler->hDel('tags', $id);
        return true;
    }
}

?>